# leo el archivo ar_properties
library(tidyverse) # libreria para data wrangling
datos1a <- read_csv("ar_properties.csv") # Acá completen con su propio PATH al archivo
[36m──[39m [1m[1mColumn specification[1m[22m [36m──────────────────────────────────────────────────────────────────────[39m
cols(
.default = col_character(),
start_date = [34mcol_date(format = "")[39m,
end_date = [34mcol_date(format = "")[39m,
created_on = [34mcol_date(format = "")[39m,
lat = [32mcol_double()[39m,
lon = [32mcol_double()[39m,
l6 = [33mcol_logical()[39m,
rooms = [32mcol_double()[39m,
bedrooms = [32mcol_double()[39m,
bathrooms = [32mcol_double()[39m,
surface_total = [32mcol_double()[39m,
surface_covered = [32mcol_double()[39m,
price = [32mcol_double()[39m
)
[36mℹ[39m Use [38;5;251m[48;5;235m[38;5;251m[48;5;235m`spec()`[48;5;235m[38;5;251m[49m[39m for the full column specifications.
datos1a # veo la base
Se seleccionan aquellos registros que pertenecen a Argentina y Capital Federal, cuyo precio esta en dolares (USD), el tipo de propiedad corresponde a Departamento y el tipo de operacion sea Venta.
datos1b <- datos1a %>%
# Me quedo con los que pertenecen a Argentina y Capital Federal
filter(l1 == "Argentina",
l2 == "Capital Federal",
# cuyo precio este en dolares
currency == "USD",
# propiedad tipo Departamento
property_type %in% c("Departamento"),
# operaciones de venta
operation_type == "Venta",
# acoto por precio y superficie
between(surface_covered,11,5000),between(price,1000,2e6)) %>% select(id,l3,surface_covered,price,lat,lon,rooms,bathrooms,bedrooms) %>% na.omit() %>% mutate(pm2=precio/sup) %>% rename(precio=price,barrio=l3,sup=surface_covered,ambientes=rooms,baths=bathrooms,cuartos=bedrooms) %>% na.omit()
# chequeo si el filtro se refleja correctamente en mi nuevo dataset datos1b
datos1b
attach(datos1b) # pongo las variables en memoria
The following objects are masked from datos1b (pos = 3):
barrio, id, lat, lon, pm2, precio, sup
The following objects are masked from datos1b (pos = 4):
barrio, id, lat, lon, pm2, precio, sup
The following objects are masked from datos1b (pos = 5):
barrio, id, lat, lon, pm2, precio, sup
The following objects are masked from datos1b (pos = 6):
barrio, id, lat, lon, pm2, precio, sup
The following objects are masked from datos1b (pos = 8):
barrio, id, lat, lon, pm2, precio, sup
The following objects are masked from datos1b (pos = 14):
barrio, id, lat, lon, pm2, precio, sup
Cargo las librerias basicas para trabajar con datos espaciales
library(terra)
library(sf)
#vignette(package = "sf")
#vignette("sf1",package = "sf")
library("leaflet")
library(tmap)
library(OpenStreetMap)
deptos.sf
Simple feature collection with 27276 features and 11 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: -58.53047 ymin: -34.69215 xmax: -58.357 ymax: -34.53613
CRS: EPSG:4326
# cargo el shape de calles de CABA
calles.comp <- st_read("/home/andresfaral/Dropbox/Estadistica Espacial/callejero/")
Reading layer `callejero' from data source
`/home/andresfaral/Dropbox/Estadistica Espacial/callejero' using driver `ESRI Shapefile'
Simple feature collection with 30712 features and 29 fields
Geometry type: LINESTRING
Dimension: XY
Bounding box: xmin: -58.53244 ymin: -34.70574 xmax: -58.34191 ymax: -34.52947
CRS: 4326
# me quedo con algunos features
calles<-calles.comp[,c("id","tipo_c","nom_mapa","long")]
plot(calles)
# selecciono solo las avenidas
avenidas<-calles[calles$tipo_c=="AVENIDA",]
plot(avenidas)
nombres.ave<-avenidas$nom_mapa
# av rivadavia
cuales<-grep("RIVADAVIA",nombres.ave)
rivadavia<-avenidas[cuales,]
plot(rivadavia)
# largo de la ave rivadavia
sum(rivadavia$long)
[1] 16623.84
# geometria de la ave rivadavia
rivadavia.geo<-rivadavia$geometry
# grafico con leaflet de avenidas
leaflet(avenidas) %>% addTiles() %>% addPolylines()
# grafico con leaflet de Rivadavia
leaflet(rivadavia) %>% addTiles() %>% addPolylines()
NA
# cargo barrios
barrios.comp <- st_read("/home/andresfaral/Dropbox/Estadistica Espacial/barrios/")
Reading layer `barrios_badata_wgs84' from data source
`/home/andresfaral/Dropbox/Estadistica Espacial/barrios' using driver `ESRI Shapefile'
Simple feature collection with 48 features and 5 fields
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -58.53152 ymin: -34.70529 xmax: -58.33515 ymax: -34.52649
CRS: 4326
barrios.comp
Simple feature collection with 48 features and 5 fields
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -58.53152 ymin: -34.70529 xmax: -58.33515 ymax: -34.52649
CRS: 4326
First 10 features:
BARRIO COMUNA PERIMETRO AREA OBJETO geometry
1 CHACARITA 15 7724.853 3115707 BARRIO POLYGON ((-58.45282 -34.595...
2 PATERNAL 15 7087.513 2229829 BARRIO POLYGON ((-58.46558 -34.596...
3 VILLA CRESPO 15 8131.857 3615978 BARRIO POLYGON ((-58.42375 -34.597...
4 VILLA DEL PARQUE 11 7705.390 3399596 BARRIO POLYGON ((-58.49461 -34.614...
5 ALMAGRO 5 8537.901 4050752 BARRIO POLYGON ((-58.41287 -34.614...
6 CABALLITO 6 10990.964 6851029 BARRIO POLYGON ((-58.43061 -34.607...
7 VILLA SANTA RITA 11 5757.752 2153691 BARRIO POLYGON ((-58.48192 -34.624...
8 MONTE CASTRO 10 7361.036 2627481 BARRIO POLYGON ((-58.50349 -34.624...
9 VILLA REAL 10 5070.966 1334146 BARRIO POLYGON ((-58.52512 -34.614...
10 FLORES 7 15772.440 8590784 BARRIO POLYGON ((-58.46041 -34.656...
plot(barrios.comp)
# me quedo solo con feature de barrio
barrios<-barrios.comp[1]
barrios
Simple feature collection with 48 features and 1 field
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -58.53152 ymin: -34.70529 xmax: -58.33515 ymax: -34.52649
CRS: 4326
First 10 features:
BARRIO geometry
1 CHACARITA POLYGON ((-58.45282 -34.595...
2 PATERNAL POLYGON ((-58.46558 -34.596...
3 VILLA CRESPO POLYGON ((-58.42375 -34.597...
4 VILLA DEL PARQUE POLYGON ((-58.49461 -34.614...
5 ALMAGRO POLYGON ((-58.41287 -34.614...
6 CABALLITO POLYGON ((-58.43061 -34.607...
7 VILLA SANTA RITA POLYGON ((-58.48192 -34.624...
8 MONTE CASTRO POLYGON ((-58.50349 -34.624...
9 VILLA REAL POLYGON ((-58.52512 -34.614...
10 FLORES POLYGON ((-58.46041 -34.656...
plot(barrios)
# la geometria
barrios$geometry
Geometry set for 48 features
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -58.53152 ymin: -34.70529 xmax: -58.33515 ymax: -34.52649
CRS: 4326
First 5 geometries:
POLYGON ((-58.45282 -34.59599, -58.45366 -34.59...
POLYGON ((-58.46558 -34.59656, -58.46562 -34.59...
POLYGON ((-58.42375 -34.59783, -58.42495 -34.59...
POLYGON ((-58.49461 -34.61487, -58.49479 -34.61...
POLYGON ((-58.41287 -34.61412, -58.41282 -34.61...
class(barrios)
[1] "sf" "data.frame"
plot(barrios[1:5,])
print(barrios.comp, n = 3)
Simple feature collection with 48 features and 5 fields
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -58.53152 ymin: -34.70529 xmax: -58.33515 ymax: -34.52649
CRS: 4326
First 3 features:
BARRIO COMUNA PERIMETRO AREA OBJETO geometry
1 CHACARITA 15 7724.853 3115707 BARRIO POLYGON ((-58.45282 -34.595...
2 PATERNAL 15 7087.513 2229829 BARRIO POLYGON ((-58.46558 -34.596...
3 VILLA CRESPO 15 8131.857 3615978 BARRIO POLYGON ((-58.42375 -34.597...
# union de barrios
union._barrios<-st_union(barrios)
union._barrios
Geometry set for 1 feature
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -58.53152 ymin: -34.70529 xmax: -58.33515 ymax: -34.52649
CRS: 4326
POLYGON ((-58.35468 -34.62922, -58.35488 -34.62...
plot(union._barrios)
# grafico con leaflet
leaflet(barrios) %>% addTiles() %>% addPolygons()
comunas.comp <- st_read("/home/andresfaral/Dropbox/Estadistica Espacial/comunas/")
Reading layer `comunas_wgs84' from data source
`/home/andresfaral/Dropbox/Estadistica Espacial/comunas' using driver `ESRI Shapefile'
Simple feature collection with 15 features and 6 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -58.53152 ymin: -34.70529 xmax: -58.33515 ymax: -34.52649
CRS: 4326
comunas.comp
Simple feature collection with 15 features and 6 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -58.53152 ymin: -34.70529 xmax: -58.33515 ymax: -34.52649
CRS: 4326
First 10 features:
ID OBJETO COMUNAS
1 1 LIMITE COMUNAL 2
2 3 LIMITE COMUNAL 6
3 6 LIMITE COMUNAL 10
4 7 LIMITE COMUNAL 11
5 8 LIMITE COMUNAL 12
6 9 LIMITE COMUNAL 14
7 11 LIMITE COMUNAL 15
8 15 LIMITE COMUNAL 13
9 13 LIMITE COMUNAL 4
10 2 LIMITE COMUNAL 5
BARRIOS PERIMETRO
1 RECOLETA 21452.84
2 CABALLITO 10990.96
3 FLORESTA - MONTE CASTRO - VELEZ SARSFIELD - VERSALLES - VILLA LURO - VILLA REAL 18332.04
4 VILLA DEL PARQUE - VILLA DEVOTO - VILLA GRAL. MITRE - VILLA SANTA RITA 19987.70
5 COGHLAN - SAAVEDRA - VILLA PUEYRREDON - VILLA URQUIZA 17232.19
6 PALERMO 22126.53
7 AGRONOMIA - CHACARITA - PARQUE CHAS - PATERNAL - VILLA CRESPO - VILLA ORTUZAR 17832.59
8 BELGRANO - COLEGIALES - NUÑEZ 26198.83
9 BARRACAS - BOCA - NUEVA POMPEYA - PARQUE PATRICIOS 35423.28
10 ALMAGRO - BOEDO 12323.43
AREA geometry
1 6317265 MULTIPOLYGON (((-58.38 -34....
2 6851029 MULTIPOLYGON (((-58.43061 -...
3 12656557 MULTIPOLYGON (((-58.48834 -...
4 14120042 MULTIPOLYGON (((-58.49838 -...
5 15570927 MULTIPOLYGON (((-58.50331 -...
6 15845871 MULTIPOLYGON (((-58.42676 -...
7 14322897 MULTIPOLYGON (((-58.42602 -...
8 14713213 MULTIPOLYGON (((-58.44033 -...
9 21684645 MULTIPOLYGON (((-58.38864 -...
10 6660603 MULTIPOLYGON (((-58.43333 -...
plot(comunas.comp)
comunas<-comunas.comp[1]
plot(comunas)
Grafico de deptos, avenidas, comunas y barrios
# todo
leaflet() %>% addTiles() %>% addPolygons(data=barrios,color = "green") %>% addPolygons(data=comunas,color = "red") %>% addCircles(data=deptos.sf,radius=0.01) %>% addPolylines(data=avenidas,color = "black")
# un poco
leaflet() %>% addTiles() %>% addPolygons(data=barrios,color = "green") %>% addPolygons(data=comunas,color = "red") %>% addCircles(data=deptos.sf[1:1000,],radius=0.01) %>% addPolylines(data=rivadavia,color = "black")
# barrios with ggplot
ggplot() + geom_sf(data = deptos.sf, size = 0.3,color="grey") + geom_sf(data = barrios, aes(fill = BARRIO), alpha = 0.5) + geom_sf(data=rivadavia)
# comunas with ggplot
ggplot() + geom_sf(data = deptos.sf, size = 0.3,color="grey") + geom_sf(data=comunas.comp, aes(fill = COMUNAS), alpha = 0.5) + geom_sf(data=rivadavia)
Que Comuna incluye a Versalles ?
#
sf::sf_use_s2(FALSE)
Spherical geometry (s2) switched off
vers<-barrios[barrios$BARRIO=="VERSALLES",]
com10<-comunas.comp[comunas.comp$COMUNAS==10,]
plot(com10$geometry)
plot(vers$geometry,add=T,col="green")
# esta versalles en la comuna 10 ?
st_contains(com10,vers,sparse = FALSE)
although coordinates are longitude/latitude, st_contains assumes that they are planar
[,1]
[1,] TRUE
# que barrios estan en que comunas
test1 <- st_intersects(comunas.comp,barrios,sparse = FALSE)
although coordinates are longitude/latitude, st_intersects assumes that they are planar
test1
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[2,] FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE
[3,] FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE
[4,] FALSE TRUE FALSE TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE
[5,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[6,] TRUE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[7,] TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[8,] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[9,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE TRUE TRUE
[10,] FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE
[11,] FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE TRUE TRUE FALSE FALSE TRUE
[12,] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE
[13,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE
[14,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
[15,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE
[,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26] [,27] [,28]
[1,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
[2,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
[3,] TRUE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[4,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE
[5,] FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE
[6,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
[7,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE TRUE TRUE TRUE
[8,] FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE
[9,] FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[10,] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
[11,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
[12,] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
[13,] TRUE TRUE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[14,] FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[15,] FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
[,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38] [,39] [,40] [,41] [,42]
[1,] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[2,] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[3,] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE FALSE
[4,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE
[5,] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE
[6,] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[7,] TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE
[8,] TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[9,] FALSE TRUE FALSE TRUE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE
[10,] FALSE FALSE FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[11,] FALSE FALSE TRUE TRUE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
[12,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
[13,] FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE FALSE FALSE
[14,] FALSE FALSE TRUE FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
[15,] FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE
[,43] [,44] [,45] [,46] [,47] [,48]
[1,] TRUE FALSE TRUE TRUE FALSE FALSE
[2,] FALSE FALSE FALSE FALSE FALSE FALSE
[3,] FALSE FALSE FALSE FALSE FALSE FALSE
[4,] FALSE FALSE FALSE FALSE FALSE FALSE
[5,] FALSE TRUE FALSE FALSE TRUE FALSE
[6,] FALSE TRUE TRUE FALSE FALSE FALSE
[7,] FALSE TRUE FALSE FALSE FALSE FALSE
[8,] FALSE TRUE FALSE FALSE TRUE FALSE
[9,] FALSE FALSE FALSE FALSE FALSE TRUE
[10,] FALSE FALSE TRUE FALSE FALSE FALSE
[11,] FALSE FALSE FALSE FALSE FALSE FALSE
[12,] TRUE FALSE TRUE FALSE FALSE FALSE
[13,] FALSE FALSE FALSE FALSE FALSE FALSE
[14,] FALSE FALSE FALSE FALSE FALSE FALSE
[15,] TRUE FALSE TRUE TRUE FALSE TRUE
Que deptos caen en versalles ?
test2 <- st_within(deptos.sf,vers,sparse = FALSE)
although coordinates are longitude/latitude, st_within assumes that they are planar
sum(test2)
[1] 25
plot(com10$geometry)
plot(vers$geometry,add=T,col="green")
plot(deptos.sf$geometry[test2],add=T,col="blue")
Que deptos estan a menos de 100 metros de Rivadavia
test3 <- st_is_within_distance(deptos.sf,rivadavia,dist = 500,sparse = FALSE)
dim(test3)
[1] 27276 169
class(test3)
[1] "matrix"
test3[1:10,1:6]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] FALSE FALSE FALSE FALSE FALSE FALSE
[2,] FALSE FALSE FALSE FALSE FALSE FALSE
[3,] FALSE FALSE FALSE FALSE FALSE FALSE
[4,] FALSE FALSE FALSE FALSE FALSE FALSE
[5,] FALSE FALSE FALSE FALSE FALSE FALSE
[6,] FALSE FALSE FALSE FALSE FALSE FALSE
[7,] FALSE FALSE FALSE FALSE FALSE FALSE
[8,] FALSE FALSE FALSE FALSE FALSE FALSE
[9,] FALSE FALSE FALSE FALSE FALSE FALSE
[10,] FALSE FALSE FALSE FALSE FALSE FALSE
cuales.deptos<-apply(test3,1,sum)
sum(cuales>0)
[1] 169
length(cuales.deptos)
[1] 27276
ggplot() + geom_sf(data = deptos.sf, size = 0.3,color="grey") + geom_sf(data = deptos.sf[cuales.deptos>0,], size = 0.5,color="blue") + geom_sf(data=rivadavia)
Combinacion de features
# uniendo tramos de rivadavia
rivadavia.unida<-st_combine(rivadavia)
rivadavia.unida
Geometry set for 1 feature
Geometry type: MULTILINESTRING
Dimension: XY
Bounding box: xmin: -58.52928 ymin: -34.63935 xmax: -58.37038 ymax: -34.6072
CRS: 4326
MULTILINESTRING ((-58.47565 -34.63232, -58.4767...
ggplot() + geom_sf(data=comunas.comp, aes(fill = COMUNAS), alpha = 0.5) + geom_sf(data=rivadavia)
ggplot() + geom_sf(data=comunas.comp, aes(fill = COMUNAS), alpha = 0.5) + geom_sf(data=rivadavia.unida)
# combinando barrios
barrios.unidos<-st_combine(barrios)
barrios.unidos
Geometry set for 1 feature
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -58.53152 ymin: -34.70529 xmax: -58.33515 ymax: -34.52649
CRS: 4326
MULTIPOLYGON (((-58.45282 -34.59599, -58.45366 ...
plot(barrios.unidos)
Que avenidas cortan Rivadavia
# Que avenidas cortan rivadavia
NoRivadavia<-avenidas[avenidas$nom_mapa!="AV. RIVADAVIA",]
test4<-st_intersects(NoRivadavia,rivadavia,sparse = FALSE)
although coordinates are longitude/latitude, st_intersects assumes that they are planar
dim(test4)
[1] 6589 169
cuales.cortan<-apply(test4,1,sum)
nombres.cortan<-NoRivadavia$nom_mapa[cuales.cortan>0]
ggplot() + geom_sf(data=NoRivadavia[NoRivadavia$nom_mapa%in%nombres.cortan,],color="blue") + geom_sf(data=rivadavia,color="red")
Que avenidas estan cerca Rivadavia
# Que avenidas cortan rivadavia
NoRivadavia<-avenidas[avenidas$nom_mapa!="AV. RIVADAVIA",]
test7<-st_is_within_distance(NoRivadavia,rivadavia,dist=1000,sparse = FALSE)
dim(test7)
[1] 6589 169
cuales.cortan<-apply(test7,1,sum)
nombres.cortan<-NoRivadavia$nom_mapa[cuales.cortan>0]
ggplot() + geom_sf(data=NoRivadavia[NoRivadavia$nom_mapa%in%nombres.cortan,],color="blue") + geom_sf(data=rivadavia,color="red")
Que avenidas hay en Caballito
test5<-st_intersects(barrios[barrios$BARRIO=="CABALLITO",],avenidas,sparse = FALSE)
although coordinates are longitude/latitude, st_intersects assumes that they are planar
dim(test5)
[1] 1 6758
test5<-as.numeric(test5)
head(test5)
[1] 0 0 0 0 0 0
ggplot() + geom_sf(data=avenidas[test5>0,],color="blue") + geom_sf(data = barrios[barrios$BARRIO=="CABALLITO",], aes(fill = BARRIO), alpha = 0.5)
Que barrios son contiguos a Caballito ?
# falla al norte
test6<-st_touches(barrios,barrios[barrios$BARRIO=="CABALLITO",],sparse = FALSE)
although coordinates are longitude/latitude, st_touches assumes that they are planar
# funciona ok
test6<-st_is_within_distance(barrios,barrios[barrios$BARRIO=="CABALLITO",],dist=100,sparse = FALSE)
dim(test6)
[1] 48 1
test6<-as.numeric(test6)
head(test6)
[1] 0 1 1 0 1 1
ggplot() + geom_sf(data = barrios[barrios$BARRIO=="CABALLITO",], aes(fill = BARRIO), alpha = 0.5) + geom_sf(data = barrios[test6>0,], aes(fill = BARRIO), alpha = 0.5)
Que deptos estan sobre avenidas ? LENTO !!!!
# test8 <- st_is_within_distance(deptos.sf,avenidas,dist = 10,sparse = FALSE)
# dim(test8)
# cuales.deptos<-apply(test8,1,sum)
# sum(cuales.deptos>0)
# ggplot() + geom_sf(data = deptos.sf, size = 0.3,color="grey") + geom_sf(data = deptos.sf[cuales.deptos>0,], size = 0.5,color="blue")
Cuantos deptos hay en cada barrio ? graficos con tmap
require(tmap)
deptos_in_barrios <- st_join(deptos.sf, barrios, join = st_within)
although coordinates are longitude/latitude, st_within assumes that they are planar
deptos_in_barrios
Simple feature collection with 27276 features and 12 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: -58.53047 ymin: -34.69215 xmax: -58.357 ymax: -34.53613
CRS: EPSG:4326
deptos_barrios_count <- count(as_tibble(deptos_in_barrios), BARRIO)
deptos_barrios_count
barrios_con_deptos <- left_join(barrios, deptos_barrios_count)
Joining, by = "BARRIO"
barrios_con_deptos
Simple feature collection with 48 features and 2 fields
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -58.53152 ymin: -34.70529 xmax: -58.33515 ymax: -34.52649
CRS: 4326
First 10 features:
BARRIO n geometry
1 CHACARITA 359 POLYGON ((-58.45282 -34.595...
2 PATERNAL 164 POLYGON ((-58.46558 -34.596...
3 VILLA CRESPO 2190 POLYGON ((-58.42375 -34.597...
4 VILLA DEL PARQUE 260 POLYGON ((-58.49461 -34.614...
5 ALMAGRO 2707 POLYGON ((-58.41287 -34.614...
6 CABALLITO 1966 POLYGON ((-58.43061 -34.607...
7 VILLA SANTA RITA 180 POLYGON ((-58.48192 -34.624...
8 MONTE CASTRO 188 POLYGON ((-58.50349 -34.624...
9 VILLA REAL 33 POLYGON ((-58.52512 -34.614...
10 FLORES 712 POLYGON ((-58.46041 -34.656...
tmap_mode("view")
tmap mode set to interactive viewing
tm_shape(barrios_con_deptos) +
tm_fill(
col = "n",
palette = "Greens",
style = "cont",
contrast = c(0.1, 1),
title = "Deptos por Barrio",
id = "boro_ct2010",
showNA = FALSE,
alpha = 0.8) +
tm_borders(col = "darkgray", lwd = 0.7)
Calculando la Capsula Convexa de los deptos
# solo los deptos
ggplot() + geom_sf(data=deptos.sf,size=0.1)
deptos.sf.unidos<-st_union(deptos.sf)
although coordinates are longitude/latitude, st_union assumes that they are planar
capsula<-st_convex_hull(deptos.sf.unidos)
capsula
Geometry set for 1 feature
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -58.53047 ymin: -34.69215 xmax: -58.357 ymax: -34.53613
CRS: EPSG:4326
POLYGON ((-58.46401 -34.69215, -58.46972 -34.69...
ggplot() + geom_sf(data=capsula,color="blue") + geom_sf(data=deptos.sf,size=0.1)
Calculemos las areas de los barrios
barrios
barrios2 <- barrios %>% mutate(AREA=st_area(barrios)) %>% arrange(-AREA)# agrego el area
Distancia entre barrios
barrios2
Simple feature collection with 48 features and 2 fields
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -58.53152 ymin: -34.70529 xmax: -58.33515 ymax: -34.52649
CRS: 4326
First 10 features:
BARRIO AREA geometry
1 PALERMO 15845872 [m^2] POLYGON ((-58.42676 -34.552...
2 VILLA LUGANO 9296146 [m^2] POLYGON ((-58.46571 -34.659...
3 VILLA SOLDATI 8688167 [m^2] POLYGON ((-58.44913 -34.651...
4 FLORES 8590788 [m^2] POLYGON ((-58.46041 -34.656...
5 BELGRANO 7999243 [m^2] POLYGON ((-58.45057 -34.535...
6 BARRACAS 7953447 [m^2] POLYGON ((-58.37034 -34.632...
7 MATADEROS 7398180 [m^2] POLYGON ((-58.50252 -34.674...
8 CABALLITO 6851031 [m^2] POLYGON ((-58.43061 -34.607...
9 VILLA DEVOTO 6404480 [m^2] POLYGON ((-58.50831 -34.613...
10 RECOLETA 6317261 [m^2] POLYGON ((-58.38 -34.57002,...
st_distance(barrios[barrios$BARRIO=="BELGRANO",],barrios[barrios$BARRIO=="FLORES",])
Units: [m]
[,1]
[1,] 4327.646
st_distance(barrios[barrios$BARRIO=="CABALLITO",],barrios[barrios$BARRIO=="FLORES",])
Units: [m]
[,1]
[1,] 0
distan<-st_nearest_points(barrios[barrios$BARRIO=="BELGRANO",],barrios[barrios$BARRIO=="FLORES",])
although coordinates are longitude/latitude, st_nearest_points assumes that they are planar
ggplot() + geom_sf(data = barrios, alpha = 0.5) + geom_sf(data = barrios[barrios$BARRIO=="FLORES",], aes(fill = BARRIO)) + geom_sf(data = barrios[barrios$BARRIO=="BELGRANO",], aes(fill = BARRIO)) + geom_sf(data=distan,col="blue")
Analisis de Espacio Verde Publico
Cual es el barrio con mas Espacio Verde ?
Estaciones de Subte
Calculemos la distancia de cada depto a la estacion de subte mas cercana
deptos.sf
Simple feature collection with 27276 features and 11 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: -58.53047 ymin: -34.69215 xmax: -58.357 ymax: -34.53613
CRS: EPSG:4326
Moelo lineal basico